//
//  MVCVC.m
//  Demo_MVC
//
//  Created by 陈冰 on 2020/3/8.
//  Copyright © 2020 ChenBing. All rights reserved.
//

#import "MVPVC.h"
#import "MVPTableViewCell.h"
#import "MVPModel.h"
#import "MVPDataSource.h"
#import "MVPPresenter.h"

static NSString *const cellRI = @"CellReuseIdentifier";
@interface MVPVC () <MVPPresenterDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) MVPDataSource *tvDataSrouce;
@property (nonatomic, strong) MVPPresenter *present;

@end

@implementation MVPVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.present = [MVPPresenter new];
    self.present.delegate = self;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MVPTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:cellRI];
    
    __weak typeof(self) weakSelf = self;
    self.tvDataSrouce = [[MVPDataSource alloc] initWithCellResueIdentifier:cellRI cellConfigBeforeBlock:^(MVPTableViewCell *cell, MVPModel *model, NSIndexPath * _Nonnull indexPath) {
        cell.nameLab.text = model.name;
        cell.numLab.text = model.num;
        cell.indexPath = indexPath;
        cell.delegate = weakSelf.present;
    } selectCellBlock:^(NSIndexPath * _Nonnull indexPath) {
        
    } reloadDataBlock:^(NSMutableArray * _Nonnull array) {
        weakSelf.present.dataArray = array;
    }];
        
    self.tableView.dataSource = self.tvDataSrouce;
    self.tableView.delegate = self.tvDataSrouce;
    
    // 异步加载数据
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 加载数据
        [self.tvDataSrouce addDataToArray:self.present.dataArray];
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            // 刷新
            [self.tableView reloadData];
        });
    });
}

- (void)reloadDataForIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@ 数据被修改", indexPath);
}

@end
